const Joi = require('joi');


/**
 * 发送验证码
 */

async function sendCodeValidate(params) {
  const schema = Joi.object({
    email: Joi.string()
      .email()
      .required()
      .empty()
      .messages({
        'string.email': '邮箱类型错误',
        'any.required': '邮箱必填',
        'string.empty': '邮箱不能为空',
      }),
    type: Joi.string().required().empty()
      .messages({
        'string.empty': '类型不能为空',
        'any.required': '类型必填',
      }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function loginValidate(params) {
  const schema = Joi.object({
    code: Joi.string()
      .required()
      .empty()
      .messages({
        'any.required': '验证码必填',
        'string.empty': '验证码不能为空',
      }),
    password: Joi.string()
      .required()
      .empty()
      .messages({
        'any.required': '密码必填',
        'string.empty': '密码不能为空',
      }),
    login_name: Joi.string()
      .required()
      .empty()
      .messages({
        'any.required': '登录名必填',
        'string.empty': '登录名不能为空',
      }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}


/**
 * 配置邮箱
 */

async function createEmailValidate(params) {
  const schema = Joi.object({
    email: Joi.string()
      .email()
      .required()
      .empty()
      .messages({
        'string.email': '邮箱类型错误',
        'any.required': '邮箱必填',
        'string.empty': '邮箱不能为空',
      }),
    code: Joi.string().required().empty()
      .messages({
        'string.empty': '验证码不能为空',
        'any.required': '验证码必填',
      }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}


/**
 * 更新邮箱
 */

async function updateEmailValidate(params) {
  const schema = Joi.object({
    old_email: Joi.string()
      .email()
      .required()
      .empty()
      .messages({
        'string.email': '旧邮箱类型错误',
        'any.required': '旧邮箱必填',
        'string.empty': '旧邮箱不能为空',
      }),
    new_email: Joi.string()
      .email()
      .required()
      .empty()
      .messages({
        'string.email': '新邮箱类型错误',
        'any.required': '新邮箱必填',
        'string.empty': '新邮箱不能为空',
      }),
    code: Joi.string().required().empty()
      .messages({
        'string.empty': '验证码不能为空',
        'any.required': '验证码必填',
      }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}


/**
 * 更新邮箱
 */

async function updatePasswordValidate(params) {
  const schema = Joi.object({
    password: Joi.string().required().empty()
      .messages({
        'string.empty': '验证码不能为空',
        'any.required': '验证码必填',
      }),
    email: Joi.string()
      .email()
      .required()
      .empty()
      .messages({
        'string.email': '新邮箱类型错误',
        'any.required': '新邮箱必填',
        'string.empty': '新邮箱不能为空',
      }),
    code: Joi.string().required().empty()
      .messages({
        'string.empty': '验证码不能为空',
        'any.required': '验证码必填',
      }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

/**
 * 创建环境
 * @param {*} params
 * @return
 */
async function envCreateValidate(params) {
  const schema = Joi.object({
    env: Joi.string().required().messages({
      'string.empty': '环境不能为空',
      'any.required': '环境名必填',
    }),
  });

  try {
    const value = await schema.validateAsync(params);

    return value;
  } catch (error) {
    throw new Error(error.message ?? '参数错误');
  }
}

/**
 * 删除环境
 * @param {*} params
 * @return
 */
async function envDeleteValidate(params) {
  const schema = Joi.object({
    env: Joi.string().required().messages({
      'string.empty': '环境不能为空',
      'any.required': '环境名必填',
    }),
  });

  try {
    const value = await schema.validateAsync(params);

    return value;
  } catch (error) {
    throw new Error(error.message ?? '参数错误');
  }
}

/**
 * 修改环境
 * @param {*} params
 * @return
 */
async function envRenameValidate(params) {
  const schema = Joi.object({
    oldName: Joi.string().required().messages({
      'string.empty': '旧环境名不能为空',
      'any.required': '旧环境名必填',
    }),
    newName: Joi.string().required().messages({
      'string.empty': '新环境名不能为空',
      'any.required': '新环境名必填',
    }),
  });

  try {
    const value = schema.validateAsync(params);
    return value;
  } catch (error) {
    throw new Error(error.message ?? '参数错误');
  }
}

/**
 * 创建应用
 * @param {*} params
 * @return
 */
async function appCreateValidate(params) {
  const schema = Joi.object({
    env: Joi.string().required().messages({
      'string.empty': '环境名不能为空',
      'any.required': '环境名必填',
    }),
    app: Joi.string().required().messages({
      'string.empty': '应用名称不能为空',
      'any.required': '应用名称必填',
    }),
  });

  try {
    const value = schema.validateAsync(params);

    return value;
  } catch (error) {
    throw new Error(error.message ?? '参数错误');
  }
}

/**
 * 删除应用
 * @param {*} params
 * @return
 */
async function appDeleteValidate(params) {
  const schema = Joi.object({
    env: Joi.string().required().messages({
      'string.empty': '环境名不能为空',
      'any.required': '环境名必填',
    }),
    app: Joi.string().required().messages({
      'string.empty': '应用名称不能为空',
      'any.required': '应用名称必填',
    }),
  });

  try {
    const value = await schema.validateAsync(params);

    return value;
  } catch (error) {
    throw new Error(error.message ?? '参数错误');
  }
}

/**
 * 修改应用
 * @param {*} params
 * @return
 */
async function appRenameValidate(params) {
  const schema = Joi.object({
    env: Joi.string().required().messages({
      'string.empty': '环境名不能为空',
      'any.required': '环境名必填',
    }),
    oldApp: Joi.string().required().messages({
      'string.empty': '旧应用名不能为空',
      'any.required': '旧应用名必填',
    }),
    newApp: Joi.string().required().messages({
      'string.empty': '新应用名不能为空',
      'any.required': '新应用名必填',
    }),
  });

  try {
    const value = await schema.validateAsync(params);

    return value;
  } catch (error) {
    throw new Error(error.message ?? '参数错误');
  }
}

/**
 * 应用列表
 * @param {*} params
 * @return
 */
async function appListValidate(params) {
  const schema = Joi.object({
    env: Joi.string().required().messages({
      'string.empty': '环境名不能为空',
      'any.required': '环境名必填',
    }),
  });

  try {
    const value = await schema.validateAsync(params);

    return value;
  } catch (error) {
    throw new Error(error.message ?? '参数错误');
  }
}


/**
 * 创建配置
 * @param {*} params
 * @return
 */
async function configCreate(params) {
  const schema = Joi.object({
    env: Joi.string().required().messages({
      'string.empty': '环境名不能为空',
      'any.required': '环境名必填',
    }),

    app: Joi.string().required().messages({
      'string.empty': '应用名称不能为空',
      'any.required': '应用名称必填',
    }),

    config: Joi.object().required().messages({
      'object.empty': '配置内容不能为空',
      'any.required': '配置内容必填',
    }),
  });
  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw new Error(error.message ?? '参数错误');
  }
}

/**
 * 更新配置
 * @param {*} params
 * @return
 */
async function configUpdate(params) {
  const schema = Joi.object({
    env: Joi.string().required().messages({
      'string.empty': '环境名不能为空',
      'any.required': '环境名必填',
    }),

    app: Joi.string().required().messages({
      'string.empty': '应用名称不能为空',
      'any.required': '应用名称必填',
    }),

    config: Joi.object().required().messages({
      'object.empty': '配置内容不能为空',
      'any.required': '配置内容必填',
    }),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw new Error(error.message ?? '参数错误');
  }
}


/**
 * 创建配置
 * @param {*} params
 * @return
 */
async function configDelete(params) {
  const schema = Joi.object({
    env: Joi.string().required().messages({
      'string.empty': '环境名不能为空',
      'any.required': '环境名必填',
    }),

    app: Joi.string().required().messages({
      'string.empty': '应用名称不能为空',
      'any.required': '应用名称必填',
    }),
  });
  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw new Error(error.message ?? '参数错误');
  }
}


/**
 * 获取列表
 * @param {*} params
 * @return
 */
async function envAndAppList(params) {

  const schema = Joi.object({
    env: Joi.string().default(''),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw new Error(error.message ?? '参数错误');
  }
}

/**
 * 获取解密后的配置详情
 * @param {*} params
 */
async function configInfo(params) {

  const schema = Joi.object({
    env: Joi.string().required().messages({
      'string.empty': '环境名不能为空',
      'any.required': '环境名必填',
    }),

    app: Joi.string().required().messages({
      'string.empty': '应用名不能为空',
      'any.require': '应用名必填',
    }),
  });


  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw new Error(error.message ?? '参数错误');
  }
}

/**
 * 获取加密后的配置详情
 * @param params
 */
async function smConfigInfo(params) {
  const schema = Joi.object({
    env: Joi.string().required().messages({
      'string.empty': '环境名不能为空',
      'any.required': '环境名必填',
    }),

    app: Joi.string().required().messages({
      'string.empty': '应用名不能为空',
      'any.require': '应用名必填',
    }),
  });


  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw new Error(error.message ?? '参数错误');
  }
}

module.exports = {
  envCreateValidate,
  envDeleteValidate,
  envRenameValidate,
  appCreateValidate,
  appDeleteValidate,
  appRenameValidate,
  configCreate,
  configUpdate,
  envAndAppList,
  configInfo,
  smConfigInfo,
  sendCodeValidate,
  createEmailValidate,
  updateEmailValidate,
  updatePasswordValidate,
  loginValidate,
  configDelete,
  appListValidate,
};
